package leetcode;

import java.util.ArrayList;

/**
 * @Author Gan
 * @Date 2021/5/23 20:36
 * @Description: 插件
 */
public class HappyNum {
    public static void main(String[] args) {
        System.out.println(isHappy(19));
    }

    public static boolean isHappy(int n) {
        ArrayList<Integer> list = new ArrayList<>();
        while (true) {
            int totalNum = 0;
            //先把数放在list中便于操作
            while (n != 0) {
                int temp = n % 10;
                list.add(temp);
                n /= 10;
            }
            for (int num : list) {
                totalNum += num * num;
            }
            if (totalNum == 1) {
                return true;
            } else {
                list.clear();
                n = totalNum;
            }
            if (totalNum < 10)
                return false;
        }
    }
}
